package java_1_21.exception;

import org.omg.CORBA.UserException;

import java.util.Scanner;

/**
 * 自定义异常
 */
public class Login {
    public static final String USER="美女";
    public static final String PASSWORD="123456";
    public static void main(String[] args) {
        try{
            login();
            System.out.println("登陆成功");

        }catch(UserNameException e){
            e.printStackTrace();
        }catch(PasswordException e){
            e.printStackTrace();
        }
    }
    public static void login() throws UserNameException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name=scanner.next();
        System.out.println("请输入密码：");
        String password= scanner.next();
        if(!name.equals(USER)){
            //用户名不对，抛出用户名错误异常
            throw new UserNameException("用户名错误");
        }
        if(!password.equals(PASSWORD)){
            //抛出密码错误异常
            throw new PasswordException("密码错误");
        }
    }
}
//用户名异常
class UserNameException extends java.lang.Exception {
    public UserNameException(String msg){
        super(msg);
    }
}
//密码异常
class PasswordException extends RuntimeException{
    public PasswordException(String msg){
        super(msg);
    }
}